// pages/goods/editProduct.js
import { update, list, find } from "../../api/product";
import { isNull } from "../../utils/util";

Page({

    /**
     * 页面的初始数据
     */
    data: {
        id: null,
        datas: {},
        sellerName: ''
    },

    /**
    * 生命周期函数--监听页面加载
    */
    onLoad(options) {
        this.data.id = options.id
        this.getGoodsInfo(options.id)
    },

    /**
    * 生命周期函数--监听页面显示
    */
    onShow() {
        let pages = getCurrentPages();
        let currPage = pages[pages.length - 1]; //当前页
        if (currPage.data.goodsId != undefined) {
            console.log(currPage.data, 'pid');
            //调取传进的参数
            this.setData({
                [`datas.goodsId`]: currPage.data.goodsId,
                [`datas.name`]: currPage.data.goodsName,
                [`datas.sn`]: currPage.data.goodsSn,
            })
        }
        if (currPage.data.memberId != undefined) {
            console.log(currPage.data, 'mid');
            //调取传进的参数
            this.setData({
                [`datas.customerId`]: currPage.data.memberId,
                sellerName: currPage.data.sellerName
            })
        }
    },

    getGoodsInfo(id) {
        let sellerName;
        find(id).then(res => {
            console.log(res);
            if (res.data.customer == null) {
                sellerName = '该卖家异常请重新选择'
            } else {
                sellerName = res.data.customer.name
            }
            this.setData({
                datas: res.data.product,
                sellerName: sellerName
            })
        })
    },



    bindPrice(e) {
        let price = 0;
        if (e.detail.value > 10000000) {
            price = 10000000;
        } else if (e.detail.value < 0) {
            price = 0
        } else {
            price = e.detail.value*1;
        }
        this.setData({
            [`datas.price`]: price
        })
    },

    bindMinimum(e) {
        let minimum;
        if (e.detail.value > 1000000) {
            minimum = 1000000;
            wx.showToast({
                title: '不能大于1000000吨',
                icon: 'none'
            })
        } else if (e.detail.value < 1) {
            minimum = 1
            wx.showToast({
                title: '不能小于1吨',
                icon: 'none'
            })
        } else {
            minimum = e.detail.value*1;
        }
        this.setData({
            [`datas.minimum`]: minimum
        })
    },

    bindDay(e) {
        let day = 0;
        if (e.detail.value >= 1000000) {
            day = 1000000;
        } else if (e.detail.value < 0) {
            day = 0
        } else {
            day = e.detail.value*1;
        }
        this.setData({
            [`datas.limitDay`]: day
        })
    },

    selectGoods() {
        wx.navigateTo({
            url: '/pages/product/selectGoods',
        })
    },

    update() {

        let _this = this.data;
        if (isNull(_this.datas.goodsId)) {
            wx.showToast({
                icon: 'none',
                title: '请选择商品',
            })
        } else if (isNull(_this.datas.customerId)) {
            wx.showToast({
                icon: 'none',
                title: '请选择卖家',
            })
        } else if (isNull(_this.datas.price)) {
            wx.showToast({
                icon: 'none',
                title: '请输入价格',
            })
        } else if (isNull(_this.datas.limitDay)) {
            wx.showToast({
                icon: 'none',
                title: '请输入锁油期限',
            })
        } else if (isNull(_this.datas.minimum)) {
            wx.showToast({
                icon: 'none',
                title: '请输入起订量',
            })
        } else {
            update(this.data.datas).then(res => {
                console.log(res);
                wx.navigateBack()
            })
        }
    },

    selectSeller() {
        wx.navigateTo({
            url: '/pages/product/seller',
        })
    },

    lowerNum() {
        let lower = this.data.datas.minimum;
        if (lower <= 1) {
            this.setData({
                [`datas.minimum`]: 1
            })
            wx.showToast({
                title: '不能小1吨',
                icon: 'none'
            })
        } else {
            this.setData({
                [`datas.minimum`]: lower - 0.5
            })
        }
    },

    addNum() {
        let lower = this.data.datas.minimum;
        if (lower >= 1000000) {
            this.setData({
                [`datas.minimum`]: 1000000
            })
            wx.showToast({
                title: '不能大于1000000吨',
                icon: 'none'
            })
        } else {
            this.setData({
                [`datas.minimum`]: lower + 0.5
            })
        }
        console.log(this.data.datas);
    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },
})
